﻿using System.Windows;
using System.Windows.Controls;

namespace Socium.Documents.Sociomatrices {
	/// <summary>
	/// Interaction logic for SociomatrixView.xaml
	/// </summary>
	public partial class SociomatrixView {
		public SociomatrixView() {
			InitializeComponent();
		}

		private void UserControlDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) {
			UpdateGrid();
		}

		private void GridLoaded(object sender, RoutedEventArgs e) {
			UpdateGrid();
		}

		void UpdateGrid() {
			var grid = UiHelper.FindChild<Grid>(this, "_grid");
			if (grid == null) {
				return;
			}

			grid.ColumnDefinitions.Clear();
			grid.RowDefinitions.Clear();

			var view_model = DataContext as SociomatrixViewModel;
			if (view_model == null) {
				return;
			}

			var auto = GridLength.Auto;

			var count = view_model.Count;
			for (var i = 0; i < count; i++) {
				var column = new ColumnDefinition {Width = auto};
				grid.ColumnDefinitions.Add(column);

				var row = new RowDefinition {Height = auto, SharedSizeGroup = "RowSizeGroup"};
				grid.RowDefinitions.Add(row);
			}
		}
	}
}
